package com.neu.his.service.registFee02.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.bo.drug.DrugItem;
import com.neu.his.bo.drug.DrugRec;
import com.neu.his.bo.drug.DrugSend;
import com.neu.his.bo.fee.FeeRec;
import com.neu.his.core.Result;
import com.neu.his.entity.DiagRecords;
import com.neu.his.entity.DrugDisturb;
import com.neu.his.entity.Patient;
import com.neu.his.mapper.DiagRecordMapper;
import com.neu.his.mapper.DrugDisturbMapper;
import com.neu.his.mapper.DrugMapper;
import com.neu.his.mapper.PatientMapper;
import com.neu.his.service.registFee02.DrugDisutrbService;
import com.neu.his.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class DrugDisturbImpl implements DrugDisutrbService {
    @Autowired
    private DrugDisturbMapper drugDisturbMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DiagRecordMapper diagRecordMapper;
    @Autowired
    private DrugMapper drugMapper;

    @Override
    public Result queryDrug(FeeRec feeRec){
        DrugSend drugSend = new DrugSend();
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Patient::getIdNumber,feeRec.getPatientId());
        List<Patient> patients = patientMapper.selectList(queryWrapper);
        if(patients.size() == 0){
            return Result.fail("病历号不存在！");
        }
        drugSend.setPatient(patients.get(0));
        QueryWrapper<DiagRecords> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(DiagRecords::getPatientId,patients.get(0).getId());
        List<DiagRecords> diagRecords = diagRecordMapper.selectList(queryWrapper2);
        List<DrugItem> list = new ArrayList<>();
        for(DiagRecords diagRecord:diagRecords){
            QueryWrapper<DrugDisturb> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(DrugDisturb::getDiagId,diagRecord.getId());
            List<DrugDisturb> drugDisturbs = drugDisturbMapper.selectList(queryWrapper1);
            for(DrugDisturb drugDisturb:drugDisturbs){
                DrugItem drugItem = new DrugItem();
                drugItem.setId(drugDisturb.getId());
                drugItem.setTotalCount(drugDisturb.getTotalCount());
                drugItem.setRealCount(drugDisturb.getRealCount());
                drugItem.setNum(drugDisturb.getNum());
                drugItem.setDrugName(drugMapper.selectById(drugDisturb.getDrugId()).getDrugName());
                if(drugDisturb.getRemoveMark() == 1){
                    drugItem.setStatus("已退");
                }else  if(drugDisturb.isHaveDeliver() == true){
                    drugItem.setStatus("已发");
                }else if(drugDisturb.getRealCount().intValue() != 0) {
                    drugItem.setStatus("未发");
                }else{
                    drugItem.setStatus("未付款");
                }
                list.add(drugItem);
            }
        }
        PageList pageList = new PageList(list,feeRec.getCurPage(),feeRec.getPageSize());
        drugSend.setData(pageList);
        return Result.success(drugSend);
    }

    @Override
    public Result deliverDrug(DrugRec drugRec){
        for(Long id : drugRec.getDrugDisturbId()){
            UpdateWrapper<DrugDisturb> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(DrugDisturb::getId,id);
            updateWrapper.set("have_deliver",true);
            drugDisturbMapper.update(null,updateWrapper);
        }
        return Result.success();
    }

    @Override
    public Result withDrawalDrug(DrugRec drugRec){
        for(Long id: drugRec.getDrugDisturbId()){
            UpdateWrapper<DrugDisturb> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(DrugDisturb::getId,id);
            updateWrapper.set("remove_mark",1);
            updateWrapper.set("have_deliver",false);
            drugDisturbMapper.update(null,updateWrapper);
        }
        return Result.success();
    }
}
